<template>
  <menu-bar class="submenu" :menus="menus" :currentPath="currentPath"></menu-bar>
  <load-more ref="listRef" :enable-refresh="false" @load="handleLoad">
    <div class="panel"
      v-for="(item, index) in currentModules"
      :key="index"
    >
      <div class="panel-heading">
        <h4 class="panel-title">{{item.title}}</h4>
        <template v-if="isChannelIndex">
          <template v-if="index === 0">
            <span class="rank" @click="handleRank(item.tid)">
              <i class="fa fa-hot"></i>排行榜 <i class="fa fa-dropright"></i>
            </span>
          </template>
          <template v-else>
            <span class="panel-more" @click="handleMore(item.tid)">
              查看更多<i class="fa fa-dropright"></i>
            </span>
          </template>
        </template>
      </div>
      <div class="panel-body">
        <video-cover
          class="vi"
          v-for="(vi, vindex) in item.data"
          :key="vindex"
          v-bind="vi"
          :play="vi.play !== undefined ? vi.play : vi.stat.view"
          :danmaku="vi.video_review !== undefined ? vi.video_review : vi.stat.danmaku"
        />
      </div>
    </div>
  </load-more>
</template>

<script>
import { computed } from 'vue'
import MenuBar from '@/components/MenuBar'
import LoadMore from '@/components/LoadMore'
import VideoCover from '@/components/VideoCover'
import useSubMenu from './hooks/useSubMenu'
import useModules from './hooks/useModules'
import checkUrl from './hooks/checkUrl'
export default {
    name: 'Channel',
    components: {
      VideoCover,
      MenuBar,
      LoadMore
    },
    props: {
      id: {
        type: [Number, String],
        required: true
      },
      subid: {
        type: [Number, String]
      }
    },
    // 校验地址是否有效
    beforeRouteEnter(to, from, next) {       
      const isValid = checkUrl(to.params)  
      if (isValid) {
        next()
      } else {
        next({ path: '404' })
      }
    },
    setup(props) {                  
      const isChannelIndex = computed(() => {        
        const id = props.id - 0
        const childId = props.subid - 0
        return id && !childId
      })
      const { menus,  currentPath } = useSubMenu(props)
      const { listRef, currentModules, handleLoad, handleMore, handleRank } = useModules(props)      
      return {
        listRef,
        menus,
        currentPath,
        isChannelIndex,
        currentModules,
        handleLoad,
        handleMore,
        handleRank
      }
    }
}
</script>

<style lang="scss" scoped>
  .submenu {
    position: sticky;
    top: 84px;
    z-index: 99;
  }
  .rank {
    color: #ffa726;
    font-size: 14px;
    .fa {
      font-size: 14px;
    }
  }
  .sub-menu :deep .van-tab{
    flex-basis: 100/4*1% !important;
  }

  .panel{
    margin: {
      top: 20px;
      bottom: 20px;
    }
    &:first-child {
      margin-top: 10px;
    }
    &-heading{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 5px 10px;
    }
    &-title{
      margin: 0;
      font-size: 15px;
      color: $color-text-regular;
      font-weight: 500;
    }
    &-more{
      color: $color-text-secondary;
      font-size: 14px;
      .fa{
        font-size: 14px;
      }
    }
    &-body{
      display: flex;
      flex-wrap: wrap;
      .vi{
        width: 50%;
        padding: 8px 5px;
        box-sizing: border-box;
      }
    }
  }
</style>